package com.qianfeng.controller;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Subject;
import com.qianfeng.enums.CommonEnum;
import com.qianfeng.exceptions.CommonException;
import com.qianfeng.service.SubjectService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getSubjectPage")
    public Result getSubjectPage(Integer pageNo, Integer pageSize) {
        Result result = new Result();
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        Page subjectPage = subjectService.getSubjectPage(pageNo, pageSize);
        System.out.println(subjectPage);
        result.setObj(subjectPage);
        result.setMessage(new Message(200, "成功"));
        return result;
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/addSubject", method = RequestMethod.POST)
    public Result addSubject(Subject subject) {
        System.out.println("添加subject" + subject);
        try {
            Result result = new Result();
            subjectService.addSubject(subject);
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.ADD_SUBJECT_ERROR);
        }
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/deleteSubject")
    public Result deleteSubject(Integer subjectId) {
        try {
            subjectService.deleteSubject(subjectId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.DELETE_SUBJECT_ERROR);
        }
    }

    /**
     * 更新
     *
     * @param
     * @return
     */
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/updateSubject", method = RequestMethod.POST)
    public Result updateSubject(Subject subject) {
        try {
            subjectService.updateSubject(subject);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.UPDATE_SUBJECT_ERROR);
        }
    }
    @RequiresAuthentication //认证通过可访问
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getSbjectById")
    public Result getSbjectById(Integer subjectId) {
        Result result = new Result();
        Subject subjectBySubjectId = subjectService.getSubjectBySubjectId(subjectId);
        result.setMessage(new Message());
        result.setObj(subjectBySubjectId);
        return result;
    }

}
